<template>
  <div class="ranking">
    <div class="ranking_tab">
      <ul class="tab_ul">
        <li
          :class="{ actives: a === index }"
          v-for="(item, index) in state.li"
          :key="item"
          @click="click(index)"
        >
          {{ item }}
        </li>
      </ul>
    </div>
    <div class="ranking_list">
      <ul>
        <li v-for="(item, index) in state.downloadlist" :key="item.id">
          <div class="id">{{ index + 1 }}</div>
          <div class="list_img">
            <img :src="item.url" alt="" />
          </div>
          <div class="list_title">
            <div class="list_div">{{ item.title }}</div>
            <div class="title_p">
              <p>
                <img src="../../../../public/img/小星星.png" alt="" />
                <span>{{ item.score }}</span>
              </p>
              <span>多人对战</span>
              <span class="dian">.</span>
              <span>MOBA</span>
            </div>
          </div>
          <div class="btn">
            <span @click="download(index)">{{
              state.downloadtitle[index]
            }}</span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import { reactive } from "vue";
import { useStore } from "vuex";
import axios from "axios";
import { ref } from "vue";
export default {
  setup() {
    const active = ref(0);
    const store = useStore();
    // const actives = ref(true);
    const a = ref(0);
    const state = reactive({
      downloadlist: [],
      downloadtitle: ["下载", "下载", "下载", "下载", "下载", "下载", "下载"],
      arr: [],
      li: ["热门榜", "预约榜", "热玩榜", "新品榜", "厂商榜"],
    });
    const click = (i) => {
      a.value = i;
      if (a.value === 1) {
        state.downloadtitle.forEach((value, index) => {
          state.downloadtitle[index] = "预约";
        });
      } else if (a.value === 2) {
        state.downloadtitle.forEach((value, index) => {
          state.downloadtitle[index] = "￥18";
        });
      } else {
        state.downloadtitle.forEach((value, index) => {
          state.downloadtitle[index] = "下载";
        });
      }
      axios
        .get("http://121.199.14.205:3000/api/list", {
          params: {
            pageSize: 3,
            currentPage: i + 1,
          },
        })
        .then(function (response) {
          state.downloadlist = response.data.list;
        });
    };
    if (localStorage.getItem("games")) {
      state.arr = localStorage.getItem("games").split(",");
    }
    axios
      .get("http://121.199.14.205:3000/api/list", {
        params: {
          pageSize: 3,
          currentPage: 1,
        },
      })
      .then(function (response) {
        state.downloadlist = response.data.list;
        if (localStorage.getItem("games")) {
          localStorage
            .getItem("games")
            .split(",")
            .forEach((item) => {
              state.downloadlist.forEach((value, index) => {
                if (item == value.id) {
                  state.downloadtitle[index] = "启动";
                }
              });
            });
        }
      });
    const download = (index) => {
      if (state.downloadtitle[index] === "下载") {
        store.commit("changehours", false);
        Toast.loading({
          message: "加载中...",
          forbidClick: true,
        });
      } else if (state.downloadtitle[index] === "启动") {
        Toast.loading({
          message: "启动中...",
          forbidClick: true,
        });
      } else if (state.downloadtitle[index] === "预约") {
        Toast("预约成功");
        state.downloadtitle[index] = "已预约";
      }
      setTimeout(() => {
        if (state.downloadtitle[index] === "下载") {
          Toast("下载成功");
          state.downloadtitle[index] = "启动";
        }
      }, 2000);
      if (state.downloadtitle[index] === "下载") {
        state.arr.push(state.downloadlist[index].id);
      }
      const arrlist = state.arr.join(",").split(","); // 转换
      const newArr = []; // 新数组
      arrlist.forEach((item, index) => {
        //新数组中若找不到
        if (newArr.indexOf(arrlist[index]) == -1) {
          newArr.push(arrlist[index]);
        }
      });
      localStorage.setItem("games", newArr);
    };

    return {
      state,
      download,
      active,
      click,
      a,
    };
  },
};
</script>

<style lang="less" scoped>
@import "../../../assets/css/var.less";
.ranking {
  position: relative;
  .ranking_tab {
    position: fixed;
    top: 55px;
    left: 0;
    background-color: #fff;
    width: 100%;
    height: 45px;
    // width: 1000px;
    // overflow-x: scroll;
    .van-tab__pane {
      width: 100px;
      height: 30px;
    }
    ul {
      width: 375px;
      display: flex;
      overflow-x: scroll;
      background-color: #ffffff;
      &::-webkit-scrollbar {
        display: none;
      }
      .actives {
        background-color: #14c5cd;
        color: white;
      }
      li {
        flex-shrink: 0;
        width: 83.5px;
        height: 37.5px;
        line-height: 37.5px;
        background-color: #f5f5f5;
        border-radius: 8px;
        text-align: center;
        margin-left: 10px;
        font-size: 14px;
        color: #999;
      }
    }
  }
  .ranking_list {
    margin-top: 45px;
    ul {
      li {
        display: flex;
        width: 100%;
        height: 88px;
        padding-top: 12px;
        .id {
          width: 30px;
          text-align: center;
          line-height: 65px;
          font-size: 20px;
        }
        .list_img {
          width: 64px;
          height: 64px;
          border-radius: 10px;
          img {
            width: 100%;
            height: 100%;
            border-radius: 10px;
          }
        }
        .list_title {
          width: 190px;
          font-size: 14px;
          .list_div {
            padding: 10px 0px 0px 10px;
            font-size: 20px;
          }
          .title_p {
            display: flex;
            .of();
            .dian {
              display: inline-block;
              width: 10px;
              text-align: center;
              line-height: 5px;
              font-size: 20px;
              color: #cccccc;
            }
            p {
              margin-left: 10px;
              img {
                width: 11px;
                height: 11px;
              }
              span {
                display: inline-block;
                width: 30px;
                text-align: center;
              }
            }
            span {
              color: #cccccc;
              padding: 10px 0px;
            }
          }
        }
        .btn {
          padding: 18px 0px;
          span {
            display: inline-block;
            width: 75px;
            height: 30px;
            line-height: 30px;
            border-radius: 15px;
            background-color: #ecfafb;
            color: #15c5ce;
            text-align: center;
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>
